﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BSCMFF.Core;
using BSCMFF.Core.DataAccessLayer;
using Telerik.Web.UI;
using BSCMFF.Web.UserControls;

namespace BSCMFF.Web.Pages.Surveys
{
    /// <summary>
    /// Report - survey
    /// </summary>
    /// <remarks></remarks>
    public partial class SurveyReport : MultiLanguagePage
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (SurveySelectionReport.PublicSelectedValue != 0)
            {
                PanelSurveyReport.Visible = true;

                BSCEntities context = new BSCEntities();

                int sfID = SurveySelectionReport.PublicSelectedValue;

                Survey_Form sf = (from sfs in context.Survey_Form
                                  where sfs.ID == sfID
                                  select sfs).SingleOrDefault();

                if (sf == null)
                    return;

                sd.Survey = sf;
                sd.DataBind();


                var data = (from srr in context.Survey_Results_Report
                            where srr.Survey_Form_FK == sfID
                            join sa in context.Survey_Answer on srr.Survey_Answer_FK equals sa.ID
                            join sq in context.Survey_Question on srr.Survey_Question_FK equals sq.ID

                            select new
                            {
                                questionID = sq.ID,
                                question = sq.Question,
                                answerID = sa.ID,
                                answer = sa.Answer,
                                number = srr.Number
                            }).ToList();

                int i = 0;
                while (i < data.Count)
                {
                    string question = data[i].question;
                    int questionID = data[i].questionID;
                    Dictionary<string, int> results = new Dictionary<string, int>();
                    results.Add(data[i].answer, data[i].number);

                    ++i;
                    while (i < data.Count && data[i].questionID == questionID)
                    {
                        results.Add(data[i].answer, data[i].number);
                        ++i;
                    }
                    SurveyQuestionChart sqch = LoadControl("~/UserControls/SurveyQuestionChart.ascx") as SurveyQuestionChart;
                    sqch.ID = questionID.ToString();
                    sqch.Question = question;
                    sqch.Results = results;
                    PanelSurveyReport.Controls.Add(sqch);
                }
            }
        }
    }
}